﻿@using Think9.Models;
@model Think9.Models.TbIndexEntity
@{
    ViewBag.Title = "Add";
    Layout = "~/Areas/Shared/_LayuiForm.cshtml";
}
<form class="layui-form" lay-filter="formUser">
    <input style="display:none" id="_tbid" value="@ViewBag.tbid">
    <input style="display:none" id="_indexid" value="@ViewBag.indexid">
    <input style="display:none" id="_frm" value="@ViewBag.Frm">

    <div class="layui-form">
        <div class="layui-form-item">
            <div class="layui-col-md3">
                <div class="layui-form-item">
                    <label class="layui-form-label">录入指标 </label>
                    <div class="layui-input-block">
                        <select id="index" name="index">
                            @foreach (valueTextEntity item in ((IEnumerable<valueTextEntity>)ViewBag.IndexList))
                            {
                                <option value="@item.Value">@item.Text</option>
                            }
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">打开页面 </label>
                    <div class="layui-input-block">
                        <select id="LinkFlag" name="LinkFlag" lay-filter="LinkFlag">
                            <option value="">==需首先选择 - 选择已定义的录入表(主表)或统计表==</option>
                            <optgroup label="已定义的录入表">
                                @foreach (valueTextEntity item in ((IEnumerable<valueTextEntity>)ViewBag.SelectList).Where(x => x.ClassID == "#table2#"))
                                {
                                    <option value="@item.Value">@item.Text</option>
                                }
                            </optgroup>
                            <optgroup label="已定义的统计表">
                                @foreach (valueTextEntity item in ((IEnumerable<valueTextEntity>)ViewBag.SelectList).Where(x => x.ClassID == "#table3#"))
                                {
                                    <option value="@item.Value">@item.Text</option>
                                }
                            </optgroup>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md3">
                <div class="layui-form-item">
                    <label class="layui-form-label">页面类型 </label>
                    <div class="layui-input-block">
                        <select name="LinkFlag2" id="LinkFlag2">
                            <option value="">==请选择打开页面类型==</option>
                            <option value="Show">Show页面</option>
                            <option value="Add">Add页面</option>
                            <option value="Edit">Edit页面</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <fieldset class="table-fieldset">
            <legend style="color:darkgrey">Add页面:传递参数可实现为打开的页面控件赋值;Show\Edit页面:传入『业务主键』值从而确定数据ID</legend>
            <div class="layui-form-item">
                <div class="layui-input-block layui-col-space3" style="margin-left:0;">
                    <div class="layui-col-xs12 layui-col-sm5 layui-col-md5">
                        <select id="ParaName" name="ParaName">
                            <option value="">==选择参数 请选择 需先选择录入表或统计表==</option>
                        </select>
                    </div>
                    <div class="layui-col-xs12 layui-col-sm5 layui-col-md5">
                        <input type="text" name="ParaValue" id="ParaValue" placeholder="请输入或选择参数值" autocomplete="off" class="layui-input">
                        <span style="position: absolute;top: 0px; left: 0px;">
                            <i class="fa fa-question-circle-o" style="color: #FE7300;" lay-tips="
@@+指标编码表示从当前数据取该指标值" data-offset="4"></i>
                        </span>
                        <span style="position: absolute;bottom: 16px; right: 5px;">
                            <a href="javascript:;">
                                <i class="fa fa-check-circle" id="selectValue" style="color: blue;" lay-tips="
选择指标值，从当前数据取值"></i>
                            </a>
                        </span>
                    </div>
                    <div class="layui-col-xs12 layui-col-sm2 layui-col-md2">
                        <button type="button" class="layui-btn" id="addPara"><i class="fa fa-plus"></i>添加参数</button>
                    </div>
                </div>
            </div>
            <div style='text-align: Left;'><span class="layui-word-tip">参数传递采用get方式，即ParameterName=ParameterValue，其中ParameterName为参数，ParameterValue为参数值，参数值如果为@@+指标编码表示从当前数据取该指标值</span></div>
            <div class="layui-form-item">
                <table class="layui-hide" id="ParaTable" lay-filter="tableFilter"></table>
            </div>
        </fieldset>
        <br>
        <div class="layui-form-item">
            <label class="layui-form-label"> </label>
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="edit">编 辑</button>
                <button type="button" class="layui-btn layui-btn-danger" id="DelLink">删 除</button>
            </div>
        </div>
    </div>

</form>
<script>
    layui.use(["table", "form", "exLayer", "exUtils"], function () {
        let table = layui.table;
        let form = layui.form;
        let layer = layui.layer;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let $ = layui.$;

        form.render(); //

        form.val("formUser", {
            "index": "@ViewBag.indexid",
            "LinkFlag": "@Model.LinkFlag",
            "LinkFlag2": "@Model.LinkFlag2"
        });

        // 添加参数值
        $('#addPara').on('click', function () {
            var _name = $("#ParaName").val();
            var _value = $("#ParaValue").val();
            var _type = "string";

            if (_name == '') {
                exLayer.yellowSighMsg("请输入参数名称");
                return false;
            }
            if (_value == '') {
                exLayer.yellowSighMsg("请选择或输入参数值");
                return false;
            }

            exUtils.ajax("/SysTable/TbSetUp/AddLinkPara", "post", { indexid: $("#_indexid").val(), name: _name, value: _value, type: _type, tbid: $("#_tbid").val() }, true).done(function (response) {
                exUtils.tableSuccessMsg(response.message);
                $("#ParaValue").val('');
                ParaTable.reload({
                    url: "/SysTable/TbSetUp/GetParaList?tbid=" + $("#_tbid").val() + '&indexid=' + $("#_indexid").val()
                });
            }).fail(function (error) {
                console.log(error);
            });
        });

        //选择参数值
        $('#selectValue').on('click', function () {
            exLayer.openMiddle("", "/SysTable/SqlTemplet/SelectIndex?tbid=" + $("#_tbid").val(), '600px', '400px', layui.device().mobile);
        });

        form.on('select(LinkFlag)', function (data) {
            var loading = layer.load('Loading...', {
                shade: [0.1, '#fff']
            });

            setTimeout(function () {
                GetSelectValueField(data.value);
                layer.close(loading);
            }, 2000);
        })

        form.on("submit(edit)", function (data) {
            exUtils.ajax("/SysTable/TbSetUp/EditIndexLink", "post", { tbid: $("#_tbid").val(), indexid: $("#_indexid").val(), linkFlag: $("#LinkFlag").val(), linkFlag2: $("#LinkFlag2").val() }, true).done(function (response) {
                $(window.parent.document).find('#LinkStr').val(response.extra);
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //删除
        $('#DelLink').on('click', function () {
            exLayer.confirm("确定要删除吗？", function (index) {
                layer.close(index);
                exUtils.ajax("/SysTable/TbSetUp/DelLink", "get", {
                    tbid: $("#_tbid").val(), indexid: $('#_indexid').val()
                }, true).done(function (response) {
                    $(window.parent.document).find('#LinkStr').val('');
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }).fail(function (error) {
                    console.log(error);
                });
            });
        });

        let ParaTable = table.render({
            elem: "#ParaTable",
            url: "/SysTable/TbSetUp/GetParaList?tbid=" + $("#_tbid").val() + '&indexid=' + $("#_indexid").val(),
            page: false,
            cols: [[
                { type: "numbers", title: "NO.", fixed: 'left' },
                { field: "ParaName", title: "参数" },
                { field: "ParaValue", title: "参数值" },
                { title: "", width: 60, align: "center", fixed: layui.device().mobile ? null : 'right', templet: "#ParaTpl" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "delPara":
                    delPara(data.Id);
                    break;
            }
        });

        function GetSelectValueField(id) {
            var controlid = "#ParaName";
            $("" + controlid + "").empty(); //清空控件
            $.getJSON("/SysTable/TbButCustomize/GetSelectValueField", { id: id }, function (data) {
                $.each(data, function (i, item) {
                    if (item.ClassID == 'err') {
                        layer.msg(item.Text, { icon: 5, time: 20000, btn: ['关闭'] });
                    }
                    else {
                        $("" + controlid + "").append(new Option(item.Text, item.Value));
                    }
                });
                form.render();//不加下拉框可能不正确显示
            });
        }

        function delPara(id) {
            exUtils.ajax("/SysTable/TbButCustomize/DeletePara", "get", { id: id }, true).done(function (response) {
                ParaTable.reload({
                    url: "/SysTable/TbSetUp/GetParaList?tbid=" + $("#_tbid").val() + '&indexid=' + $("#_indexid").val()
                });
            }).fail(function (error) {
                console.log(error);
            });
        }

        //消息提示
        $(document).on("mouseenter", "*[lay-tips]", function () {
            var remind = $(this).attr("lay-tips");
            var tips = $(this).data("offset") || 4;
            var color = $(this).data("color") || '#88858e';
            layer.tips(remind, this, {
                time: -1,
                tips: [tips, color],
                area: ['auto', 'auto'],
            });
        }).on("mouseleave", "*[lay-tips]", function () {
            layer.closeAll("tips");
        });

        $(document).ready(function () {
            GetSelectValueField($("#LinkFlag").val());
        });
    });
</script>
<script type="text/html" id="ParaTpl">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delPara" id="delPara"><i class="fa fa-close"></i></a>
</script>